<?php
include("general_include.php");

function OrderNumberGenerator($tablename,$field)
{
	$OrderNumber="";
	function make_seed()
	{
	   list($usec, $sec) = explode(' ', microtime());
	   return (float) $sec + ((float) $usec * 100000);
	}
	while(!$OrderNumber)

	{
			mt_srand(make_seed());
			$randval = mt_rand();
			$randvalw=rand(65,90);
			$randvalw=chr($randvalw);
			$OrderNumber=$randvalw."-".$randval;
			$select_number="SELECT * FROM $tablename WHERE $field = '$OrderNumber' ";
			$result_number=mysql_query($select_number) or die("Error In Generating OrderNumber ".mysql_error());
			$num=mysql_num_rows($result_number);
			if($num>0)
				$OrderNumber="";

	}
	return  $OrderNumber;
}
if(isset($_POST['submit'])){	  
		$ret = array();
      $Formval = $_POST; 
	  $email = trim($_POST['email']);
	  $password1 = trim($_POST['password1']);
	  $password2 = trim($_REQUEST['Password2']);
	  $name = trim($_REQUEST['name']);
	  $lang_count=count($_POST['language']);
	  $city = trim($_POST['city']);
	  $cityid = trim($_POST['cityid']);
	  $skype_id = trim($_POST['skype_id']);
	  $code = trim(strtolower($_POST['txtCaptcha']));
	  
	  //Language 
	  for($l=0; $l<$lang_count; $l++)
	  {
	    $language_id.= $language[$l].',';
	  }
	  $language_ids = substr($language_id,0,-1);
	  $valid = 1;
	 
	  // Availability Type
	  $dupemail= "SELECT * FROM ".TABLEPREFIX."_user where email='$email'";
	  $rowArr = $UserManagerObjAjax->GetRecords("Row",$dupemail);
	  
	  if($rowArr['user_id']<>''){
	      $ermsg="This email id is already registered";
		  $valid=0;
		  $ret['IsSuccess'] = false;
		  $ret['Message'] = $ermsg;
	  }
	  
	  $dupnm= "SELECT * FROM ".TABLEPREFIX."_user where name='$name'";
	  $rowArr1 = $UserManagerObjAjax->GetRecords("Row",$dupnm);
	  
	  if($rowArr1['user_id']<>'' && $valid ==1)
	  {
	      $ermsg="This UserID is already registered";
		  $valid=0;
		  $ret['IsSuccess'] = false;
		  $ret['Message'] = $ermsg;
	  }
	 if($code != $_SESSION['captcha'] && $valid ==1)
	  {
	      $ermsg="Enter security code correctly";
		  $valid=0;
		  $ret['IsSuccess'] = false;
		  $ret['Message'] = $ermsg;
	  }
	  if($valid==1)
	  {
		
	     $code = OrderNumberGenerator(TABLEPREFIX.'_user','registration_number');
		 $sqlCity = "SELECT * FROM ".TABLEPREFIX."_cities WHERE city_id='$cityid'";
		 $rowCity = $UserManagerObjAjax->GetRecords("Row",$sqlCity);
		 $country = $rowCity['country_id'];
		 $Sql = "INSERT INTO ".TABLEPREFIX."_user set  registration_number='$code',email='$email',name='$name',password='$password1',city='$cityid',country='$country',
		 skype_id='$skype_id',date_registered='".date('Y-m-d H:i:s')."',language_ids='$language_ids'";
		 //die($Sql);
	     $UserManagerObjAjax->Execute($Sql);
		 $user_id=mysql_insert_id();	
		 $Sql1 = "INSERT INTO " . TABLEPREFIX . "_user_type_user_relation set user_id='$user_id',user_type_id='2',date_added='".date('Y-m-d H:i:s')."'";
		 $UserManagerObjAjax->Execute($Sql1); 
		 
		 if(!mysql_error())
		 {
		    unset($_SESSION['captcha']);
		    $_SESSION['new_reg_id'] = $user_id;
	
			$_SESSION['user_id']=$_SESSION['new_reg_id'];
			$_SESSION['name'] = $name;
			$ret['IsSuccess'] = true;
			
		 }

	  } 
		
		echo json_encode($ret);
}

?>